/* eslint-disable guard-for-in */
import * as BASE from '@/stores/definitions-base';
import { Checkbox, DatePicker2, Input } from '@alifd/next';
import { t } from '@lingui/macro';
import React from 'react';
import AreaCascader from '../AreaCascader';
import MapModalInput from '../MapModalInput';
import DriverInput from '../DriverInput';

const {
  DATETIME_TEXT,
  DATE_TEXT,
  TIME_TEXT,
  EMPTY_TEXT,
  ENUMS: ENUMSBase,
  FieldsGrid: FieldGridBase,
  TitleGrid,
  column: columnBase,
  toSelectDataSource,
} = BASE;
export { DATETIME_TEXT, DATE_TEXT, EMPTY_TEXT, TIME_TEXT, TitleGrid, toSelectDataSource };

export function column(...args) {
  return columnBase(orderModel, ...args);
}

export function FieldsGrid(props) {
  return <FieldGridBase model={orderModel} {...props} />;
}
//----------------------------------------------------------------------
export const ENUMS = {
  ...ENUMSBase,
  orderType: {
    0: t`点对点`,
    1: t`时租`,
  },
  language: {
    1: t`普通话`,
    2: t`粤语`,
    3: t`英语`,
  },
  orderStatus: {
    0: t`已取消`,
    1: t`已支付`,
    2: t`进行中`,
    3: t`已完成`,
  },
  compareTitle: {
    passengerName: '【基本信息】>【订单信息】乘坐人：',
    passengerPhone: '【基本信息】>【订单信息】乘坐人电话：',
    number: '【基本信息】>【乘坐信息】人数：',
    predictBeginDate: '【基本信息】>【乘坐信息】预估开始时间：',
    flightNumber: '【基本信息】>【乘坐信息】航班号：',
    startMainArea: '【基本信息】>【乘坐信息】开始城市：',
    startMinorArea: '【基本信息】>【乘坐信息】开始区域：',
    beginName: '【基本信息】>【乘坐信息】开始详情地址：',
    endMainArea: '【基本信息】>【乘坐信息】结束城市：',
    endMinorArea: '【基本信息】>【乘坐信息】结束区域：',
    endName: '【基本信息】>【乘坐信息】结束详情地址：',
    predictTime: '【基本信息】>【乘坐信息】租车时长：',
    priceDifference: '【基本信息】>【乘坐信息】订单差价：',
    language: '【基本信息】>【乘坐信息】语言要求：',
    orderRemarks: '【基本信息】>【附加费】订单备注：',
    companyName: '【承运信息】所属公司：',
    surcharges: '【基本信息】>【附加费】>【附加服务】：',
    driverName: '【承运信息】司机名：',
    mainland: '【承运信息】大陆车牌：',
    hk: '【承运信息】香港车牌：',
    carrierPrice: '【承运信息】>公司费用：',
    driverServiceFee: '【承运信息】>司机费用：',
    taskRemark: '【承运信息】>派车备注：',
  },
};

export const MAGIC_NUMBERS = {
  // 订单状态  0-已取消 1-已支付 2-进行中 3-已完成
  ORDER_STATE: {
    CANCEL: 0,
    PAID: 1,
    START: 2,
    FINISH: 3,
  },
  /** 分账状态
   * 类型
   * 1 - 正常
   * 2 - 分账异常禁止提交
   * 3 - 分账异常需要登记支出款项
   */
  SPLIT_STATUS: {
    SUCCESS: '1',
    NO_SUBMIT: '2',
    NEED_REMARK: '3',
  },
};

export const orderModel = {
  crDate: {
    label: t`下单时间`,
    text: DATETIME_TEXT,
  },
  ccOrder: {
    orderType: {
      label: t`订单类型`,
      text: (v, record) => {
        const { orderType } = record?.ccOrder;
        return ENUMS.orderType[orderType];
      },
    },
    subscribeName: {
      label: t`预约人`,
    },
    subscribePhone: {
      label: t`预约人电话`,
    },
  },
  passengerName: {
    label: t`乘坐人`,
  },
  passengerPhone: {
    label: t`乘坐人电话`,
    ui_edit: {
      itemProps: {
        validator: (_, value) => {
          // 自定义手机号校验规则
          const phoneRegex = /^[1-9]\d{10}$/; // 匹配手机号的正则表达式

          if (!value || value.match(phoneRegex)) {
            return Promise.resolve();
          }

          return Promise.reject(t`请输入有效的手机号码！`);
        },
      },
    },
  },
  number: {
    label: t`人数`,
  },
  predictBeginDate: {
    label: t`预估开始时间`,
    ui_edit: {
      renderer: (v) => {
        return (
          <DatePicker2
            showTime
            value={v}
            format={DATETIME_TEXT}
            hasClear={false}
            timePanelProps={{
              format: 'HH:mm',
              defaultValue: v,
              minuteStep: 5,
            }}
          />
        );
      },
    },
  },
  flightNumber: {
    label: t`航班号`,
  },
  beginArea: {
    label: t`开始区域`,
    text: (v, record) => {
      const { beginAreaName = '' } = toJS(record)?.ORDER || {};
      return beginAreaName;
    },
    ui_edit: {
      renderer: (v, record, ref) => {
        const { path: name, ...cfg } = ref || {};
        return (
          <AreaCascader
            value={v}
            type="begin"
            name={name}
            record={toJS(record)}
            {...cfg}
          />
        );
      },
    },
  },
  beginName: {
    label: t`开始详情地址`,
    ui_edit: {
      renderer: (v, record, ref) => {
        const { path: name, ...cfg } = ref || {};
        return (
          <MapModalInput
            value={v}
            name={name}
            type="begin"
            record={toJS(record)}
            {...cfg}
          />
        );
      },
      span: 3,
    },
    ui_view: {
      span: 3,
    },
  },
  endArea: {
    label: t`结束区域`,
    text: (v, record) => {
      const { endAreaName = '' } = toJS(record)?.ORDER || {};
      return endAreaName;
    },
    ui_edit: {
      renderer: (v, record, ref) => {
        const { path: name, ...cfg } = ref || {};
        return (
          <AreaCascader
            value={v}
            type="end"
            record={toJS(record)}
            name={name}
            {...cfg}
          />
        );
      },
    },
  },
  endName: {
    label: t`结束详情地址`,
    ui_edit: {
      renderer: (v, record, ref) => {
        const { path: name, ...cfg } = ref || {};
        return (
          <MapModalInput
            value={v}
            name={name}
            type="end"
            record={toJS(record)}
            {...cfg}
          />
        );
      },
      span: 3,
    },
    ui_view: {
      span: 3,
    },
  },
  language: {
    label: t`语言要求`,
    ui_edit: {
      renderer: (v) => {
        const checkboxList = [];
        for (const key in ENUMS.language) {
          checkboxList.push({
            label: ENUMS.language[key],
            value: Number(key),
          });
        }
        return (
          <Checkbox.Group
            dataSource={checkboxList}
            value={v}
          />
        );
      },
      span: 3,
    },
    ui_view: {
      span: 3,
    },
  },
  remarks: {
    label: t`订单备注`,
    ui_edit: {
      renderer: (v) => (
        <Input.TextArea
          value={v}
          placeholder={t`请输入订单备注`}
        />
      ),
      span: 4,
    },
    ui_view: {
      span: 4,
    },
  },
  driver: {
    label: t`司机名`,
    ui_edit: {
      renderer: (v, record, ref) => {
        const { path: name, ...cfg } = ref || {};
        return (
          <DriverInput
            name={name}
            value={v}
            record={record}
            {...cfg}
          />
        );
      },
      span: 2,
    },
    ui_view: {
      span: 2,
    },
  },
  licenseNo: {
    mainland: {
      label: t`大陆车牌`,
    },
    hk: {
      label: t`港澳车牌`,
    },
  },
  carrierPrice: {
    label: t`公司费用`,
  },
  driverServiceFee: {
    label: t`司机费用`,
  },
  taskRemark: {
    label: t`派车备注`,
    ui_edit: {
      renderer: () => (
        <Input.TextArea
          placeholder={t`请输入派车备注`}
        />
      ),
      span: 4,
    },
    ui_view: {
      span: 4,
    },
  },
  dispatchOrderRemarks: {
    label: t`派单备注`,
    ui_edit: {
      renderer: () => (
        <Input.TextArea
          placeholder={t`给公司调度员留言`}
        />
      ),
      span: 4,
    },
    ui_view: {
      span: 4,
    },
  },
  priceDifference: {
    label: t`订单差价`,
    ui_edit: {
      itemProps: {
        validator: (_, value) => {
          // 自定义数字校验规则
          const numberRegex = /^-?[0-9]+$/; // 匹配数字的正则表达式
          if (!value || value.match(numberRegex)) {
            return Promise.resolve();
          }
          return Promise.reject(t`请输入有效的数字！`);
        },
      },
    },
  },
};
